package com.maszy.db;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ProxySelector;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.util.Vector;

public class DBConnection{

	private Statement stmt;

	private Connection con;

	private ResultSet rst;
	
	private PreparedStatement pst;
	
	public DBConnection(){
		
	}
	public Connection getConnection() throws Exception{
		
		try{
			con = ConnectionManager.getConnection();
			
		}catch(Exception e){
			
		}
		return con;
		
	}


	public ResultSet executeQuery(String SqlStr,PreparedStatement pst) throws Exception {
		ResultSet result = null;
		try {
			con.setAutoCommit(false);
			stmt = con.createStatement();
//			result = stmt.executeQuery(SqlStr);
			pst = con.prepareStatement(SqlStr);
			result = pst.executeQuery();
			con.commit();
		} catch (java.sql.SQLException e) {
			System.out.println(SqlStr);
			throw new Exception("");
		}
		return result;
	}
	/**
	 * ִ�и���
	 */
	public int Update(String SqlStr,PreparedStatement pst) throws Exception {
		int result = -1;
		try {
			con.setAutoCommit(false);
			pst = con.prepareStatement(SqlStr);
			pst.executeQuery();
			con.commit();
			if (result == 0){
				System.out.println(SqlStr);
				
			}
		} catch (java.sql.SQLException e) {
			System.out.println(SqlStr);
		
		}
		return result;
	}
	/**
	 * ִ�в�ѯ����2
	 */
	public ResultSet Query(String SqlStr) throws Exception {
		
		try {
			
			con.setAutoCommit(false);
			stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.TYPE_SCROLL_INSENSITIVE);
			rst = stmt.executeQuery(SqlStr);
			con.commit();
		} catch (java.sql.SQLException e) {			

		}
		return rst;
	}



	/**
	 * ִ��������
	 */
	public boolean handleTransaction(Vector SqlArray) throws Exception {
		boolean result = false;
		int ArraySize = SqlArray.size();
		try {
			stmt = con.createStatement();
			con.setAutoCommit(false);
			System.out.println("ArraySize is" + ArraySize);
			for (int i = 0; i < ArraySize; i++) {
				System.out.println(" ��ʼִ�����" + (String) SqlArray.elementAt(i));
				stmt.executeUpdate((String) SqlArray.elementAt(i));
				System.out.println(" ִ�гɹ�");
			}
			con.commit();
			con.setAutoCommit(true);// ����
			System.out.println("����ִ�гɹ�");
			result = true;
		} catch (java.sql.SQLException e) {
			try {
				System.out.println(e.toString());
				System.out.println("��ݿ����ʧ��");
				con.rollback();
			} catch (java.sql.SQLException Te) {
				
			}
		}
		try {
			con.setAutoCommit(true);
		} catch (java.sql.SQLException e) {
			
		}
		return result;
	}

	/**
	 * �ͷ�l��
	 */
	public void close() throws Exception {
		try {
			if (stmt != null)
				stmt.close();
			if (con != null) {
				ConnectionManager.closeConnection(con);
				

			}
		} catch (java.sql.SQLException e) {
			System.err.println("�ͷ�l�ӳ��");
		}
	}
	
	public  void sendupdate(String server_ip, int server_port, String message) {
		System.out.println("send msg!!!!!"+server_ip+":"+server_port);
		DatagramSocket socket=null;
		DatagramPacket op = null ;
		InetAddress target = null;
		try {
			socket =  new DatagramSocket(10001);
			target = InetAddress.getByName(server_ip);
			byte[] buf = message.getBytes();
			op = new DatagramPacket(buf, buf.length, target, server_port);
			System.out.println("message="+message);
			socket.send(op);
			System.out.println("111111111111111111111 send over");
		} catch (IOException ex) {
			System.out.print(ex.getMessage());
		}finally{
			socket.close();
		}

	}
	public Connection getCon() {
		return con;
	}
	public void setCon(Connection con) {
		this.con = con;
	}
}
